/**
 * 工具函数
 */

// 创建一个公共的foot组键
function tabs(n) {
  let foot = document.querySelector(".foot");
  foot.innerHTML = `
    <ul>
      <li class="${n === 0 ? 'active' : ''}">
        <a href="./index.html">
        <i class="iconfont icon-shouyecopy"></i>
        <a class="as">首页</a>
        </a>
      </li>
      <li class="${n === 1 ? 'active' : ''}">
        <a href="./exercise.html">
        <i class="iconfont icon-ziyuan5"></i>
        <a class="as">运动</a>
        </a>
      </li>
      <li class="${n === 2 ? 'active' : ''}">
        <a href="./mine.html">
        <i class="iconfont icon-wode"></i>
        <a class="as">我的</a>
        </a>
      </li>
    </ul>
    `
  document.body.appendChild(foot);
}


//封装一个拦截登录的函数
function checkLogin() {
  const token = window.localStorage.getItem('token')
  const user = window.localStorage.getItem('user')
  if (!token && !user) {
    location.href = './login.html'
  }
}


//封装一个提示的组件
function toast(status, msg, fn) {
  const div = document.createElement('div')
  div.classList.add('show-toast')
  div.innerHTML = `
  <i>${status === 0 ? '√' : 'X'}</i>
  <p>${msg}</p>
  `
  document.body.appendChild(div)
  setInterval(function () {
    div.remove()
    fn && fn();
  }, 2000)
}

//处理时间的函数
function dateForm(str) {
  var now = new Date(str);
  // console.log(now);
  // 年
  let y = now.getFullYear();
  // 月
  let mon = ((now.getMonth() + 1).toString()).padStart(2,"0");
  // 日
  let d = (now.getDate().toString()).padStart(2,"0");
  // 时
  let h = (now.getHours().toString()).padStart(2,"0");
  // 分
  let m = (now.getMinutes().toString()).padStart(2,"0");
  // 秒
  let s = (now.getSeconds().toString()).padStart(2,"0");

  return `${y}-${mon}-${d} ${h}:${m}:${s}`
}


window.$utils = {
  tabs: tabs,
  checkLogin: checkLogin,
  toast: toast,
  dateForm:dateForm
}